package com.bo.activity;

import com.bo.activity.R;
import com.bo.entity.CompanyTouch;
import com.bo.entity.Mark;
import com.bo.util.LoadResourceProxy;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TextView;

public class CompanyTouchActivity extends MyBaseActivity{
	
	final int BEFORE_COLOR=Color.GRAY;
	final int AFTER_COLOR=Color.BLUE;
	
	private TextView address,webSite,phone,fax,email,title;
	private ImageView imageView;
	
	private CompanyTouch companyTouch=null; 
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		this.setContentView(R.layout.company_touch);
		
		    
		 String companyId=this.getIntent().getStringExtra("companyId");
		 String companyName=this.getIntent().getStringExtra("companyName");
		
		try {
			
			companyTouch=new CompanyTouch(companyId).createCompanyTouch(companyId);
			
		} catch (Exception e) {
			
			Log.i("error",e.getMessage());
		}
		

		TableLayout layout=(TableLayout)this.findViewById(R.id.cpt_table);
		
		
		title=(TextView)this.findViewById(R.id.cpt_title);
		
		title.setText(companyName);
		
		imageView=(ImageView)this.findViewById(R.id.cpt_img);
		
		imageView.setImageBitmap(LoadResourceProxy.getImage(LoadResourceProxy.COMPANY_TOUCH
				            ,companyTouch.getId()+"",companyTouch.getId(),1, companyTouch.getImage()));
		
		
        phone=(TextView)this.findViewById(R.id.cpt_phone);

        phone.setText(companyTouch.getPhone());
        
        phone.setOnTouchListener(new OnTouchListener(){

			public boolean onTouch(View v, MotionEvent event) {
                 
				phone.setBackgroundColor(AFTER_COLOR);
				v.invalidate();

				return false;
			}
        });
       
       fax=(TextView)this.findViewById(R.id.cpt_fax);
		
       if(companyTouch.getFax().equals("")){
        	
        	layout.removeView(this.findViewById(R.id.cpt_faxRow));
        
       }else{
        	
        	fax.setText(companyTouch.getFax());
        }
        
        
        webSite=(TextView)this.findViewById(R.id.cpt_webSite);
        
        if(companyTouch.getWebSite().equals("")){
        	
        	layout.removeView(this.findViewById(R.id.cpt_webSiteRow));
        	
        }else{
        	
        	webSite.setText("http://"+companyTouch.getWebSite());
        	
        	webSite.setOnTouchListener(new OnTouchListener(){

     			public boolean onTouch(View v, MotionEvent event) {
                      
     				webSite.setBackgroundColor(AFTER_COLOR);
     				v.invalidate();

     				return false;
     			}
             });
        }
		
        email=(TextView)this.findViewById(R.id.cpt_email);
        
        if(companyTouch.getEmail().equals("")){
        	
        	layout.removeView(this.findViewById(R.id.cpt_emailRow));
        }else{
        	
        	email.setText(companyTouch.getEmail());
        	email.setOnTouchListener(new OnTouchListener(){

     			public boolean onTouch(View v, MotionEvent event) {
                      
     				email.setBackgroundColor(AFTER_COLOR);
     				v.invalidate();

     				return false;
     			}
             });
        }

		address=(TextView)this.findViewById(R.id.cpt_address);
		
		address.setText(companyTouch.getAddress());
			
			address.setOnTouchListener(new OnTouchListener(){

				public boolean onTouch(View v, MotionEvent event) {
					
					address.setBackgroundColor(AFTER_COLOR);
					v.invalidate();
					
					Intent intent=new Intent(CompanyTouchActivity.this,CompanyMapActivity.class);
					
					Bundle data=new Bundle();
				
					data.putDouble("latitude", companyTouch.getLatitude());
					data.putDouble("longitude", companyTouch.getLongitude());
					
					intent.putExtras(data);
					
					startActivity(intent);
					
					return false;
				}
		});
	}
	
	
	
	@Override
	public void onStart(){
		
		super.onStart();
		
		email.setBackgroundColor(BEFORE_COLOR);
		address.setBackgroundColor(BEFORE_COLOR);
		phone.setBackgroundColor(BEFORE_COLOR);
		webSite.setBackgroundColor(BEFORE_COLOR);
		
	}


	
	@Override
	public boolean onCreateOptionsMenu(Menu menu){
		
		return this.menuInit(menu, true);
	}
	
	

	@Override
	protected Mark getMark() {

		return null;
	}

}
